﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Reflection;

    [ScriptType("SP.PropertyValues", ServerTypeId="{3973524d-2d5a-4683-aa39-38a2acc6e63c}")]
    public class PropertyValues : ClientObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public PropertyValues(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [PseudoRemote]
        internal object GetFieldValue(string fieldName)
        {
            object obj2 = null;
            if (!this.FieldValues.TryGetValue(fieldName, out obj2))
            {
                throw new PropertyOrFieldNotInitializedException();
            }
            return obj2;
        }

        protected override void InitNonPropertyFieldFromJson(string peekedName, JsonReader reader)
        {
            KeyValuePair<string, object> pair = reader.ReadKeyValue();
            this.FieldValues[pair.Key] = pair.Value;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }

        protected override void LoadExpandoFields()
        {
            foreach (string str in this.FieldValues.Keys)
            {
                base.Retrieve(new string[] { str });
            }
        }

        public override void RefreshLoad()
        {
            base.RefreshLoad();
            this.LoadExpandoFields();
        }

        [Remote]
        internal void SetFieldValue(string fieldName, object value_)
        {
            this.FieldValues[fieldName] = value_;
            ClientAction query = new ClientActionInvokeMethod(this, "SetFieldValue", new object[] { fieldName, value_ });
            if (base.Context != null)
            {
                base.Context.AddQuery(query);
            }
        }

        public Dictionary<string, object> FieldValues
        {
            get
            {
                Dictionary<string, object> dictionary = null;
                object obj2 = null;
                if (base.ObjectData.MethodReturnObjects.TryGetValue("$m_dict", out obj2))
                {
                    dictionary = obj2 as Dictionary<string, object>;
                }
                if (dictionary == null)
                {
                    dictionary = new Dictionary<string, object>();
                    base.ObjectData.MethodReturnObjects["$m_dict"] = dictionary;
                }
                return dictionary;
            }
        }

        public object this[string fieldName]
        {
            [PseudoRemote(Name="GetFieldValue")]
            get
            {
                return this.GetFieldValue(fieldName);
            }
            [Remote(Name="SetFieldValue")]
            set
            {
                this.SetFieldValue(fieldName, value);
            }
        }
    }
}

